* Important note: The results in columns 1-3 of Table A3 are produced directly from the (Diagnosis) heterogeneity analysis in Appendix F. 
* YOU MUST run do-file 'AppF_make_diagnosis_data' first before this file to produce results.


* Produce Appendix Table A3 (column 4)

* First calculate number of affected patients for each condition
use "$saveddata/data_complete_withcosts.dta", clear

replace d1 = 40 if d1==.

keep if finyear==2012
keep if bin>170 & bin<410

gen one=1

collapse (sum) one, by(d1_all)

rename one pat_number

egen total = sum(pat_number)

save "saveddata/affected_numbers.dta", replace

* Outsheet results
outsheet using "$results/tableA3_col4.csv", comma replace

** Aggregate impact is total patients * mortality reduction (0.0041) = 20,400 lives saved

* Merge in these affected patient numbers with diagnosis specific impacts, calculate shares, output results as Appendix Table A3

use "$inputddata/het_fulldiag_data.dta", clear

* Appendix Table A3 (columns 1-3) can be obtained directly from these outputs.
keep d1 wt1 diff_death30 predmort

merge 1:1 d1 using "$saveddata/affected_numbers.dta"

replace wt1 = -wt1
gsort -diff_death30

replace diff_death30 = diff_death30*100
replace predmort = predmort*100

order d1 wt1 diff_death predmort

gen lives_saved = (diff_death30/100)*pat_number

* Drop those without diagnosis info
drop if d1==38 | d1==40

egen total_saved = sum(lives_saved)
gen share_saved = lives_saved/total_saved

keep d1 wt1 diff_death30 predmort pat_number share_saved

drop if wt1==.
replace share_saved = share_saved*100

* Outsheet results for Table A3
outsheet using "$results/tableA3.csv", comma replace
